﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace CRM
{
    public partial class UserPowerEdit : System.Web.UI.Page
    {
        protected override void OnPreLoad(EventArgs e)
        {
            Common.CheckMes.CheckState("User_ID", "Index.aspx", "用户登录信息丢失，请重新登录...");
            string PowerMes = Common.EncryptString.DeMyEnncrypt(Common.GetMes.GetCookies("Power_Mes"), 6);

            if (PowerMes.IndexOf("2_2") < 0)
            {
                Common.MessageBox.Show(Page, "你没有编辑的权限...", "Body.aspx", "BACK");
            }
            base.OnPreLoad(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(Common.GetMes.GetRequestQuery(Request, "PID")))
            {
                SqlDataReader DataRead = BLL.ServiceUser.GetPowerInfo(Common.GetMes.GetRequestQuery(Request, "PID"));
                if (DataRead.HasRows)
                {
                    DataRead.Read();
                    txtPowerName.Value = DataRead["T_PowerName"].ToString().Trim();
                    txtRemark.Value = DataRead["T_Remark"].ToString().Trim();
                    SetPowerMes(DataRead["T_PowerMes"].ToString());
                }
                DataRead.Close();
                DataRead.Dispose();
            }
        }

        protected void BtnSaveUserInfo_Click(object sender, EventArgs e)
        {
            //实例化T_ServicePower类，用来作为UpdatePowerMes的参数
            Model.T_ServicePower PowerModel = new CRM.Model.T_ServicePower();
            //获取查询字符串中传入的角色Id值
            PowerModel.T_PowerID = (string.IsNullOrEmpty
                (Common.GetMes.GetRequestQuery(Request, "PID"))) ? "" 
                : Common.GetMes.GetRequestQuery(Request, "PID");
            //得到角色名称
            PowerModel.T_PowerName = txtPowerName.Value.Trim();
            PowerModel.T_Remark = txtRemark.Value.Trim();
            //得到角色权限字符串列表
            PowerModel.T_PowerMes = GetPosweMes();
            //调用UpdatePowerMes更新角色
            if (BLL.ServiceUser.UpdatePowerMes(PowerModel))
            {   //根据T_PowerID判断是编辑还是新增
                if (string.IsNullOrEmpty(PowerModel.T_PowerID))
                {  //如果保存成功，则显示提示信息，并根据用户选择导航页面
                    Common.MessageBox.myScriptMes(Page, 
                        "if(confirm(\"信息保存成功，是否继续添加？否则返回人员列表。\")) "+
                        "{location.href=\"UserPowerEdit.aspx\"} else  "+
                        "{location.href=\"UserPowerManage.aspx\"} ", "Suess");
                }
                else
                {  //显示编辑成功消息
                    Common.MessageBox.Show(Page, "信息修改成功...", 
                        "UserPowerManage.aspx", "Suess2");
                }
                
            }
            else 
            {   //显示保存错误消息
                Common.MessageBox.Show(Page, 
                    "信息保存失败，请重试或联系技术人员......", 
                    "UserPowerEdit.aspx", "Error");
            }
        }

        /// <summary>
        /// 获取用户的权限
        /// </summary>
        private string GetPosweMes()
        {
            string strValue = string.Empty;
            if (CBUserSee.Checked)
            {
                strValue += "1_1,";
            }
            if (CBUserEdit.Checked)
            {
                strValue += "1_2,";    
            }
            if (CBUserDel.Checked)
            {
                strValue += "1_3,";
            }

            if (CBServiceSee.Checked)
            {
                strValue += "2_1,";
            }
            if (CBServiceEdit.Checked)
            {
                strValue += "2_2,";
            }
            if (CBServiceDel.Checked)
            {
                strValue += "2_3,";
            }

            if (CBPersonSee.Checked)
            {
                strValue += "3_1,";
            }
            if (CBPersonEdit.Checked)
            {
                strValue += "3_2,";
            }
            if (CBPersonDel.Checked)
            {
                strValue += "3_3,";
            }
            strValue = (strValue.Length > 0) ? strValue.Substring(0, strValue.Length - 1) : strValue;
            return strValue;
        }

        /// <summary>
        /// 设置权限选中项
        /// </summary>
        /// <param name="strPowerMes"></param>
        private void SetPowerMes(string strPowerMes)
        {
            if (!string.IsNullOrEmpty(strPowerMes.Trim()))
            {
                string[] strPower = strPowerMes.Split(',');
                for (int i = 0; i < strPower.Length; i++)
                {
                    if (strPower[i].Trim()=="1_1")
                    {
                        CBUserSee.Checked = true;
                    }
                    if (strPower[i].Trim()=="1_2")
                    {
                        CBUserEdit.Checked = true;
                    }
                    if (strPower[i].Trim() == "1_3")
                    {
                        CBUserDel.Checked = true;
                    }

                    if (strPower[i].Trim() == "2_1")
                    {
                        CBServiceSee.Checked = true;
                    }
                    if (strPower[i].Trim() == "2_2")
                    {
                        CBServiceEdit.Checked = true;
                    }
                    if (strPower[i].Trim() == "2_3")
                    {
                        CBServiceDel.Checked = true;
                    }


                    if (strPower[i].Trim() == "3_1")
                    {
                        CBPersonSee.Checked = true;
                    }
                    if (strPower[i].Trim() == "3_2")
                    {
                        CBPersonEdit.Checked = true;
                    }
                    if (strPower[i].Trim() == "3_3")
                    {
                        CBPersonDel.Checked = true;
                    }
                }
            }
        }
    }
}
